//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "YearOnYearInflationSwapHelper.h"
using namespace Cephei::QL::Termstructures::Inflation;
#include <gen/QL/Quote.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Indexes/YoYInflationIndex.h>
#include <gen/QL/Termstructures/YoYInflationTermStructure.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Inflation::CYearOnYearInflationSwapHelper::CYearOnYearInflationSwapHelper (Cephei::QL::IQuote^ quote, Cephei::QL::Times::IPeriod^ swapObsLag_, DateTime maturity, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum paymentConvention, Cephei::QL::Times::IDayCounter^ dayCounter, Cephei::QL::Indexes::IYoYInflationIndex^ yii) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CQuote^ _Cquote;
    CPeriod^ _CswapObsLag_;
    CCalendar^ _Ccalendar;
    CDayCounter^ _CdayCounter;
    CYoYInflationIndex^ _Cyii;
    try
    {
#ifdef HANDLE
        _phYearOnYearInflationSwapHelper = NULL;
#endif
        _Cquote = safe_cast<CQuote^> (quote);
        _Cquote->Lock();
        Handle<QuantLib::Quote>& _quote = static_cast<Handle<QuantLib::Quote>&> (_Cquote->GetHandle ()); 
        _CswapObsLag_ = safe_cast<CPeriod^> (swapObsLag_);
        _CswapObsLag_->Lock();
        QuantLib::Period& _swapObsLag_ = static_cast<QuantLib::Period&> (_CswapObsLag_->GetReference ()); 
        QuantLib::Date _maturity = (QuantLib::Date)ValueHelper::Convert (maturity); //d
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::BusinessDayConvention _paymentConvention = (QuantLib::BusinessDayConvention)paymentConvention ;
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _Cyii = safe_cast<CYoYInflationIndex^> (yii);
        _Cyii->Lock();
        boost::shared_ptr<QuantLib::YoYInflationIndex>& _yii = static_cast<boost::shared_ptr<QuantLib::YoYInflationIndex>&> (_Cyii->GetShared ()); 
        _ppYearOnYearInflationSwapHelper = new boost::shared_ptr<QuantLib::YearOnYearInflationSwapHelper> (new QuantLib::YearOnYearInflationSwapHelper ( _quote,  _swapObsLag_,  _maturity,  _calendar,  _paymentConvention,  _dayCounter,  _yii ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cquote != nullptr) _Cquote->Unlock();
        if (_CswapObsLag_ != nullptr) _CswapObsLag_->Unlock();
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
        if (_Cyii != nullptr) _Cyii->Unlock();
    }
}
Cephei::QL::Termstructures::Inflation::CYearOnYearInflationSwapHelper::CYearOnYearInflationSwapHelper (boost::shared_ptr<QuantLib::YearOnYearInflationSwapHelper>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYearOnYearInflationSwapHelper = NULL;
#endif
	_ppYearOnYearInflationSwapHelper = &childNative;
    
}
Cephei::QL::Termstructures::Inflation::CYearOnYearInflationSwapHelper::CYearOnYearInflationSwapHelper (QuantLib::YearOnYearInflationSwapHelper& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phYearOnYearInflationSwapHelper = NULL;
#endif
	_ppYearOnYearInflationSwapHelper = new boost::shared_ptr<QuantLib::YearOnYearInflationSwapHelper> (&childNative);
    
    _YearOnYearInflationSwapHelperOwner = owner;
    
}

Cephei::QL::Termstructures::Inflation::CYearOnYearInflationSwapHelper::CYearOnYearInflationSwapHelper (CYearOnYearInflationSwapHelper^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYearOnYearInflationSwapHelper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppYearOnYearInflationSwapHelper = new boost::shared_ptr<QuantLib::YearOnYearInflationSwapHelper> (copy->GetShared());
        
    }
}
Cephei::QL::Termstructures::Inflation::CYearOnYearInflationSwapHelper::CYearOnYearInflationSwapHelper (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYearOnYearInflationSwapHelper = NULL;
#endif
	if (!t->IsSubclassOf(CYearOnYearInflationSwapHelper::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Inflation::CYearOnYearInflationSwapHelper::CYearOnYearInflationSwapHelper (QuantLib::Handle<QuantLib::YearOnYearInflationSwapHelper>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phYearOnYearInflationSwapHelper = &childNative;
	_ppYearOnYearInflationSwapHelper = &static_cast<boost::shared_ptr<QuantLib::YearOnYearInflationSwapHelper>>(childNative.currentLink());
    
    _YearOnYearInflationSwapHelperOwner = owner;
}
Cephei::QL::Termstructures::Inflation::CYearOnYearInflationSwapHelper::CYearOnYearInflationSwapHelper (QuantLib::Handle<QuantLib::YearOnYearInflationSwapHelper> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phYearOnYearInflationSwapHelper = &childNative;
	_ppYearOnYearInflationSwapHelper = &static_cast<boost::shared_ptr<QuantLib::YearOnYearInflationSwapHelper>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Inflation::CYearOnYearInflationSwapHelper::CYearOnYearInflationSwapHelper (QuantLib::YearOnYearInflationSwapHelper childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYearOnYearInflationSwapHelper = NULL;
#endif
	_ppYearOnYearInflationSwapHelper = new boost::shared_ptr<QuantLib::YearOnYearInflationSwapHelper> (new QuantLib::YearOnYearInflationSwapHelper (childNative));
    
}
#endif

Cephei::QL::Termstructures::Inflation::CYearOnYearInflationSwapHelper::~CYearOnYearInflationSwapHelper ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppYearOnYearInflationSwapHelper != NULL)
    {
	    delete _ppYearOnYearInflationSwapHelper;
        _ppYearOnYearInflationSwapHelper = NULL;
    }
}
Cephei::QL::Termstructures::Inflation::CYearOnYearInflationSwapHelper::!CYearOnYearInflationSwapHelper ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppYearOnYearInflationSwapHelper != NULL)
    {
	    delete _ppYearOnYearInflationSwapHelper;
    }
}
QuantLib::YearOnYearInflationSwapHelper& Cephei::QL::Termstructures::Inflation::CYearOnYearInflationSwapHelper::GetReference ()
{
    if (_ppYearOnYearInflationSwapHelper == NULL) throw REFNEW NativeNullException ();
	return **_ppYearOnYearInflationSwapHelper;
}
boost::shared_ptr<QuantLib::YearOnYearInflationSwapHelper>& Cephei::QL::Termstructures::Inflation::CYearOnYearInflationSwapHelper::GetShared ()
{
    if (_ppYearOnYearInflationSwapHelper == NULL) throw REFNEW NativeNullException ();
	return *_ppYearOnYearInflationSwapHelper;
}
QuantLib::YearOnYearInflationSwapHelper* Cephei::QL::Termstructures::Inflation::CYearOnYearInflationSwapHelper::GetPointer ()
{
    if (_ppYearOnYearInflationSwapHelper == NULL) throw REFNEW NativeNullException ();
	return &**_ppYearOnYearInflationSwapHelper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::YearOnYearInflationSwapHelper>& Cephei::QL::Termstructures::Inflation::CYearOnYearInflationSwapHelper::GetHandle ()
{
	if (_phYearOnYearInflationSwapHelper == NULL)
	{
		_phYearOnYearInflationSwapHelper = new Handle<QuantLib::YearOnYearInflationSwapHelper> (*_ppYearOnYearInflationSwapHelper);
	}
	return *_phYearOnYearInflationSwapHelper;
}
#endif
bool Cephei::QL::Termstructures::Inflation::CYearOnYearInflationSwapHelper::HasNative () 
{
	return (_ppYearOnYearInflationSwapHelper != NULL);
}

Double Cephei::QL::Termstructures::Inflation::CYearOnYearInflationSwapHelper::ImpliedQuote::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppYearOnYearInflationSwapHelper)->impliedQuote ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Inflation::IYearOnYearInflationSwapHelper^ Cephei::QL::Termstructures::Inflation::CYearOnYearInflationSwapHelper::SetTermStructure (Cephei::QL::Termstructures::IYoYInflationTermStructure^ y)
{
    CYoYInflationTermStructure^ _Cy;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cy = safe_cast<CYoYInflationTermStructure^> (y);
        _Cy->Lock();
        QuantLib::YoYInflationTermStructure* _y = static_cast<QuantLib::YoYInflationTermStructure*> (_Cy->GetPointer ()); 
    	(*_ppYearOnYearInflationSwapHelper)->setTermStructure ( _y );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cy != nullptr) _Cy->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Inflation::IYearOnYearInflationSwapHelper^ Cephei::QL::Termstructures::Inflation::CYearOnYearInflationSwapHelper_Factory::Create (Cephei::QL::IQuote^ quote, Cephei::QL::Times::IPeriod^ swapObsLag_, DateTime maturity, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum paymentConvention, Cephei::QL::Times::IDayCounter^ dayCounter, Cephei::QL::Indexes::IYoYInflationIndex^ yii)
{
    return REFNEW CYearOnYearInflationSwapHelper ( quote,  swapObsLag_,  maturity,  calendar,  paymentConvention,  dayCounter,  yii);
}
